package net.nightwhistler.htmlspanner.handlers;

import android.text.SpannableStringBuilder;

import net.nightwhistler.htmlspanner.FontFamily;
import net.nightwhistler.htmlspanner.SpanStack;
import net.nightwhistler.htmlspanner.TagNodeHandler;
import net.nightwhistler.htmlspanner.spans.VerticalMarginSpan;
import net.nightwhistler.htmlspanner.style.Style;
import net.nightwhistler.htmlspanner.style.Style.TextAlignment;
import net.nightwhistler.htmlspanner.style.StyleCallback;
import net.nightwhistler.htmlspanner.style.StyleValue;

import org.htmlcleaner.TagNode;

/**
 * TagNodeHandler for any type of text that may be styled using CSS.
 * 
 * @author Alex Kuiper
 */
public class StyledTextHandler extends TagNodeHandler {

	private Style style;
	protected boolean useCssStyle = false;

	public void setUseCssStyle(boolean useCssStyle) {
		this.useCssStyle = useCssStyle;
	}

	protected boolean useCssStyle() {
		return useCssStyle;
	}

	public StyledTextHandler() {
		this.style = new Style();
	}

	public StyledTextHandler(Style style, boolean useCssStyle) {
		this.useCssStyle = useCssStyle;
		this.style = style;
	}

	public Style getStyle() {
		return style;
	}
	FontFamily family;
	@Override
	public void beforeChildren(TagNode node, SpannableStringBuilder builder,
			SpanStack spanStack) {
		Style useStyle = getStyle();
		if (useCssStyle()) {
			useStyle = spanStack.getStyle(node, getStyle());
		}
		if(node.getName().equals("div") 
				&& node.hasAttribute("class")
				&& node.getAttributeByName("class").equals("someonesaid")){
			builder.append("\n");
		}
		
		// 判断样式是否需要换行
		if (builder.length() > 0
				&& useStyle.getDisplayStyle() == Style.DisplayStyle.BLOCK) {

			if (builder.charAt(builder.length() - 1) != '\n') {
				builder.append('\n');
			}
		}

		// 设置Top margin，如果有topmargin属性，加入一个新行
		// If we have a top margin, we insert an extra newline. We'll manipulate
		// the line height
		// of this newline to create the margin.
		if (useStyle.getMarginTop() != null) {

			StyleValue styleValue = useStyle.getMarginTop();

			if (styleValue.getUnit() == StyleValue.Unit.PX) {
				if (styleValue.getIntValue() > 0) {
					if (appendNewLine(builder)) {
						spanStack
								.pushSpan(
										new VerticalMarginSpan(styleValue
												.getIntValue()), builder
												.length() - 1, builder.length());
					}
				}
			} else {
				if (styleValue.getFloatValue() > 0f) {
					if (appendNewLine(builder)) {
						spanStack.pushSpan(
								new VerticalMarginSpan(styleValue
										.getFloatValue()),
								builder.length() - 1, builder.length());
					}
				}
			}

		}

//		if(node.getName().equals("strong")){
//			builder.append("\n");
//		}
	}

	public final void handleTagNode(TagNode node,
			SpannableStringBuilder builder, int start, int end,
			SpanStack spanStack) {

		Style styleFromCSS = getStyle();
		if (useCssStyle()) {
			styleFromCSS = spanStack.getStyle(node, getStyle());
		}
		if(node.getName().equals("p") 
				&& node.hasAttribute("class")
				&& node.getAttributeByName("class").equals("text-right")){
			styleFromCSS = styleFromCSS.setTextAlignment(TextAlignment.RIGHT);
		}
		handleTagNode(node, builder, start, end, styleFromCSS, spanStack);
	}

	public void handleTagNode(TagNode node, SpannableStringBuilder builder,
			int start, int end, Style useStyle, SpanStack stack) {

		if (useStyle.getDisplayStyle() == Style.DisplayStyle.BLOCK) {
			appendNewLine(builder);

			// If we have a bottom margin, we insert an extra newline. We'll
			// manipulate the line height
			// of this newline to create the margin.
			if (useStyle.getMarginBottom() != null) {

				StyleValue styleValue = useStyle.getMarginBottom();

				if (styleValue.getUnit() == StyleValue.Unit.PX) {
					if (styleValue.getIntValue() > 0) {
						appendNewLine(builder);
						stack.pushSpan(
								new VerticalMarginSpan(styleValue.getIntValue()),
								builder.length() - 1, builder.length());
					}
				} else {
					if (styleValue.getFloatValue() > 0f) {
						appendNewLine(builder);

						stack.pushSpan(
								new VerticalMarginSpan(styleValue
										.getFloatValue()),
								builder.length() - 1, builder.length());
					}
				}

			}
		}

		stack.pushSpan(new StyleCallback(getSpanner().getFontResolver()
				.getDefaultFont(), useStyle, start, builder.length()));
	}

}
